<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model {
	
	
	
	
	public function __construct() {
		parent::__construct();
	}
	
	
	
	
	
	/**
	* get information about the specific user (admin,school or hotel) 
	* @param $id User identification
	* @return $result an object with all User information
	*/
	public function getUserInfo($id) {
		if (isset($id)) {
			$this->db->select('*');
			$this->db->from('users');
			$this->db->where('users.id',$id);
			$this->db->join('meta','users.id = meta.user_id');
			//$query = $this->db->get();
			//$result = $query->result();
			$result = $this->db->get()->row();
			return $result;
			//return $result['0'];
		}
		else {
			return null;
		}
	}
	
	
	/**
	 * Get the 'offer_id', 'name', 'price', 'level', 'number_of_classes' and the number of available spots of
	 * all available and active offers from the school with the given 'id'
	 * @param $schoolId 
	 * @return $result An array of objects with the fields (offer_id,name,price,level, number_of_classes, futureClasses, spot)
	 */
	public function getAvailableOffers($schoolId) {
	
		$now = date('Y-m-d H:i:s',time());
	
		$select = '
			users_id 						as schoolId,
			meta.company 					as schoolName,
			offer.id						as offer_id,
			class.id						as classId,
			name,
			price,
			level,
			numberOfClasses,
			(class.maxPart - class.part) 	as spots,
			count(*) 						as futureClasses
		';
		$this->db->select($select);
		$this->db->where('users_id',$schoolId);
		$this->db->where('remainingClasses',0);
		$this->db->where('offer.active',1);
		$this->db->from('offer');
		$this->db->join('class','class.offer_id = offer.id');
		$this->db->join('schedule','schedule.offer_id = offer.id');
		$this->db->join('users','users.id = offer.users_id');
		$this->db->join('meta','meta.user_id = users.id');
	
		$this->db->where('end >', $now);
		$this->db->group_by("offer_id");
	
		$query = $this->db->get();
	
		$result = $query->result();
		
		return $result;
	}
	
	
	/**
	 * get all classes from the given schoolId that only have schedules above 'now' 
	 * @param $schoolId
	 */
	public function getAvailableClasses_($schoolId) {
		
		$now = date('Y-m-d H:i:s',time());
		
		// future
		$this->db->select('class_id')->from('schedule');
		$this->db->join('class','class.id = schedule.class_id','left outer');
		$this->db->where('start >', $now);
		$this->db->group_by('class_id');
		$result = $this->db->get()->result();
		if (empty($result))
			return $result;
		$futureSchedulesClassIDs = array();
		foreach ($result as $item) {
			array_push($futureSchedulesClassIDs, $item->class_id);
		}
		
		// ... in execution
		$this->db->select('class_id')->from('schedule');
		$this->db->join('class','class.id = schedule.class_id','left outer');
		$this->db->where('start <', $now);
		$this->db->where_in('class_id',$futureSchedulesClassIDs);
		$this->db->group_by('class_id');
		$result = $this->db->get()->result();
		$inExecutionClassIDs = array();
		foreach ($result as $item) {
			array_push($inExecutionClassIDs, $item->class_id);
		}
		
		// future - inExecution = available classes
		$availableClasses = array_diff($futureSchedulesClassIDs, $inExecutionClassIDs);
		
		//$this->firephp->log($availableClasses,'availableClasses');
		
		$select = '
			offer.users_id 					as schoolId,
			meta.company 					as schoolName,
			offer.id						as offer_id,
			class.id						as class_id,
			offer.name						as name,
			offer.price						as price,
			offer.level						as level,
			offer.maxPart					as maxPart,
			offer.numberOfClasses			as numberOfClasses,
			(class.maxPart - class.part) 	as spots
		';
		$this->db->select($select);
		$this->db->where('users_id',$schoolId);
		//$this->db->where('remainingClasses',0);
		//$this->db->where('offer.active',1);
		$this->db->from('offer');
		$this->db->join('class','class.offer_id = offer.id');
		//$this->db->join('schedule','schedule.offer_id = offer.id');
		$this->db->join('users','users.id = offer.users_id');
		$this->db->join('meta','meta.user_id = users.id');
		$this->db->where_in('class.id',$availableClasses);
		$result = $this->db->get()->result();
		
		return $result;
	}
	
	private function classIDsWithSchedulesAfterNow() {
		
		$user_id = $this->ion_auth->get_user()->id;
		
		$now = date('Y-m-d H:i:s',time());
		
		$this->db->select('class_id, count(*) as qt');
		$this->db->from('schedule');
		$this->db->where('start >',$now);
		$this->db->join('offer','offer.id = schedule.offer_id');
		$this->db->join('users','users.id = offer.users_id');
		$this->db->where('users.id',$user_id);
		$this->db->group_by('class_id');
		$result = $this->db->get()->result();
		
		$classIDs = array();
		foreach ( $result as $item ) {
			//array_push($classIDs, $item->class_id);
			$classIDs[$item->class_id] = $item->qt;
		}
		
		return $classIDs;
	}
	

	
	private function get_all_available_classes($schoolIDs = null) {
		
		$user_id = $this->ion_auth->get_user()->id;
		
		$now = date('Y-m-d H:i:s',time());
		
		$select = 
		'
			offer.users_id 					as schoolId,
		    meta.company					as schoolName,
		    offer.id						as offer_id,
		    class.id						as class_id,
			offer.name						as name,
			offer.price						as price,
			offer.level						as level,
			offer.maxPart					as maxPart,
			offer.numberOfClasses			as numberOfClasses,
		    (class.maxPart - class.part)	as spots
		';
		
		$this->db->select($select);
		
		$this->db->from('offer');
		
		$this->db->join('class','class.offer_id = offer.id');
		$this->db->join('users','users.id = offer.users_id');
		$this->db->join('meta','meta.user_id = users.id');
		
		$this->db->where_in('users.id',array(3,7));
		$this->db->where_in('class.id',array(38,39));
		
		$result = $this->db->get()->result();
		
		return $result;
	}
		
	
	/**
	 * Retrieve an array of objects with all clients from the current logged in user.
	 */
	public function getClient() {
		
		$users_id = $this->ion_auth->get_user()->id;
		$this->db->select('*');
		$this->db->from('client');
		$this->db->where('users_id',$users_id);
		$query = $this->db->get();
		$rows = $query->result();
		return $rows;
	}	
	
	
	/**
	 * retrieve all client IDs from the current user with a reservation in the given class 
	 * @param $classId
	 * @return array with client IDs. Or an empty array if there is no clients
	 */
	public function getClientIDsInClass($classId) {
	
		$users_id = $this->ion_auth->get_user()->id;
	
		$clientIdList = array();
		
		$this->db->select('reserve.client_id');
		$this->db->from('reserve');
		$this->db->join('client','client.id = reserve.client_id','inner');
		$this->db->join('users','users.id = client.users_id','inner');
		$this->db->where('class_id', $classId);
		$this->db->where('users.id', $users_id);
		$result = $this->db->get()->result();
	
		if ($result == null)
			return $clientIdList;
	
		foreach ($result as $item) {
			array_push($clientIdList, $item->client_id);
		}
	
		return $clientIdList;
	}
	
	
	
	
	/**
	 * Check if the given client from the current logged in user is already in the reservation class 
	 * @param  $clientId
	 * @param  $classId
	 */
	public function isClienteInReservationList($clientId, $classId) {
	
		$clientsInClass = $this->getClientIDsInClass($classId);
	
		if (in_array($clientId, $clientsInClass)) {
			return true;
		}
		else {
			return false;
		}
	}
	
	/**
	 * check if the class is full
	 * @param $classId
	 */
	public function isClassFull($classId) {
		$class = $this->db->select('maxPart, part')->from('class')->where('id',$classId)->get()->row();
 		if ($class->part == $class->maxPart)
 			return true;
 		else 
 			return false;
	}
	
	
	
}